﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GomMinhLongDO;
using GomMinhLongBL;

namespace GomMinhLong
{
    public partial class Article : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int ArticleID = int.Parse(Request.QueryString["ID"].ToString());
            DataTable dtArticle = new DataTable("dtArticle");

            if (!IsPostBack)
            {
                dtArticle = new cmsArticleBL().SelectByPK(ArticleID);
                if ((dtArticle != null) && (dtArticle.Rows.Count > 0))
                {
                    Session["TitleArticle"] = dtArticle.Rows[0]["Title"].ToString();
                    Page.Title = Session["TitleArticle"].ToString();
                    HtmlMeta metaDesc = new HtmlMeta();
                    metaDesc.Name = "description";
                    metaDesc.Content = dtArticle.Rows[0]["Description"].ToString();
                    Page.Header.Controls.Add(metaDesc);

                    HtmlMeta metaKey = new HtmlMeta();
                    metaKey.Name = "keywords";
                    metaKey.Content = dtArticle.Rows[0]["Tags"].ToString();
                    Page.Header.Controls.Add(metaKey);
                    lblTitle.Text = dtArticle.Rows[0]["Title"].ToString();
                    lblInfo.Text = dtArticle.Rows[0]["ArticleDetail"].ToString();

                    int CategoryID = int.Parse(dtArticle.Rows[0]["CategoryID"].ToString());
                    this.Bindrpt(ArticleID, CategoryID);
                }
                else
                {
                    Page.Title = Ultility.PageTitle();
                    lblTitle.Text = "Nội dung bạn yêu cầu không tìm thấy!";
                }
            }
        }

        public void Bindrpt(int ArticleID, int CategoryID)
        {
            DataTable dtArticleCorrelative = new DataTable("dtArticleCorrelative");
            dtArticleCorrelative = new cmsArticleBL().SelectArticleCorrelative(ArticleID, CategoryID);
            if ((dtArticleCorrelative != null) && (dtArticleCorrelative.Rows.Count > 0))
            {
                rpt.DataSource = dtArticleCorrelative;
                rpt.DataBind();
            }
        }

        public string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }

        public string Change_AV(string ip_str_change)
        {
            return Ultility.Change_AV(ip_str_change);
        }

        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0)
            {
                return;
            }
            DataRowView itemData = (DataRowView)e.Item.DataItem;
            HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");

            string P = "";
            P = Request.QueryString["Page"];
            if (P == "Chi-Tiet-Tin-Tuc")
            {
                if (hplTitle != null)
                {
                    hplTitle.NavigateUrl = "/Chi-Tiet-Tin-Tuc/" + itemData["ArticleID"].ToString() + "/" + FriendlyUrl(itemData["Title"].ToString());
                    hplTitle.Text = "» " + itemData["Title"].ToString();
                    hplTitle.ToolTip = itemData["Title"].ToString();
                }
            }
            else if (P == "Chi-Tiet-Khuyen-Mai")
            {
                if (hplTitle != null)
                {
                    hplTitle.NavigateUrl = "/Chi-Tiet-Khuyen-Mai/" + itemData["ArticleID"].ToString() + "/" + FriendlyUrl(itemData["Title"].ToString());
                    hplTitle.Text = "» " + itemData["Title"].ToString();
                    hplTitle.ToolTip = itemData["Title"].ToString();
                }
            }
        }
    }
}